package com.ruoyi.product.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.coupon.api.RemoteCouponService;
import com.ruoyi.coupon.api.domain.MemberPrice;
import com.ruoyi.coupon.api.domain.SkuFullReduction;
import com.ruoyi.coupon.api.domain.SkuLadder;
import com.ruoyi.coupon.api.domain.SpuBounds;
import com.ruoyi.product.domain.*;
import com.ruoyi.product.dto.BaseAttrs;
import com.ruoyi.product.dto.ProductDTO;
import com.ruoyi.product.dto.Skus;
import com.ruoyi.product.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.service.ISpuInfoService;
import org.springframework.util.CollectionUtils;

/**
 * spu信息Service业务层处理
 * 
 * @author 汤小洋
 * @date 2023-11-23
 */
@Service
@RequiredArgsConstructor
public class SpuInfoServiceImpl implements ISpuInfoService 
{
    private final SpuInfoMapper spuInfoMapper;
    private final SpuInfoDescMapper spuInfoDescMapper;
    private final SpuImagesMapper spuImagesMapper;
    private final SpuAttrValueMapper spuAttrValueMapper;

    private final SkuInfoMapper skuInfoMapper;
    private final SkuImagesMapper skuImagesMapper;
    private final SkuAttrValueMapper skuAttrValueMapper;

    private final RemoteCouponService remoteCouponService;

    /**
     * 查询spu信息
     * 
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public SpuInfo selectSpuInfoById(Long id)
    {
        return spuInfoMapper.selectSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     * 
     * @param spuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<SpuInfo> selectSpuInfoList(SpuInfo spuInfo)
    {
        return spuInfoMapper.selectSpuInfoList(spuInfo);
    }

    /**
     * 新增spu信息
     * 
     * @param spuInfo spu信息
     * @return 结果
     */
    @Override
    public void insertSpuInfo(ProductDTO productDTO)
    {
        /**
         * 操作spu相关信息
         */
        // spuInfo
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(productDTO, spuInfo);
        spuInfo.setCreateTime(new Date());
        spuInfo.setUpdateTime(new Date());
        spuInfoMapper.insertSpuInfo(spuInfo);

        Long spuId = spuInfo.getId();

        // spuInfoDesc
        SpuInfoDesc spuInfoDesc = new SpuInfoDesc();
        spuInfoDesc.setSpuId(spuId);
        spuInfoDesc.setDecript(productDTO.getDecript());
        spuInfoDescMapper.insertSpuInfoDesc(spuInfoDesc);

        // spuImages
        String imagesStr = productDTO.getImages();
        if (StringUtils.isNotEmpty(imagesStr)){
            Stream.of(imagesStr.split(",")).forEach(item -> {
                SpuImages spuImages = new SpuImages();
                spuImages.setSpuId(spuId);
                spuImages.setImgUrl(item);
                spuImagesMapper.insertSpuImages(spuImages);
            });
        }
        // spuAttrValue
        List<BaseAttrs> baseAttrs = productDTO.getBaseAttrs();
        if(! CollectionUtils.isEmpty(baseAttrs)){
            baseAttrs.forEach(item -> {
                SpuAttrValue spuAttrValue = new SpuAttrValue();
                spuAttrValue.setSpuId(spuId);
                spuAttrValue.setAttrId(item.getAttrId());
                spuAttrValue.setAttrName(item.getAttrName());
                spuAttrValue.setAttrValue(item.getAttrValues());
                spuAttrValue.setQuickShow(item.getShowDesc());
                spuAttrValueMapper.insertSpuAttrValue(spuAttrValue);
            });
        }

        /**
         * 操作sku相关信息
         */
        List<Skus> skus = productDTO.getSkus();
        skus.forEach(item -> {
            // skuInfo
            SkuInfo skuInfo = new SkuInfo();
            skuInfo.setSpuId(spuId);
            skuInfo.setSkuName(item.getSkuName());
            skuInfo.setCategoryId(spuInfo.getCategoryId());
            skuInfo.setBrandId(spuInfo.getBrandId());
            String defaultImage = item.getImages().stream().filter(img -> img.getDefaultImg() == 1).findFirst().get().getImgUrl();
            skuInfo.setSkuDefaultImg(defaultImage);
            skuInfo.setSkuTitle(item.getSkuTitle());
            skuInfo.setSkuSubtitle(item.getSkuSubtitle());
            skuInfo.setPrice(BigDecimal.valueOf(item.getPrice()));
            skuInfoMapper.insertSkuInfo(skuInfo);

            Long skuId = skuInfo.getSkuId();

            // skuImages
            item.getImages().stream()
                    .filter(subItem -> StringUtils.isNotEmpty(subItem.getImgUrl()))
                    .forEach(subItem -> {
                        SkuImages skuImages = new SkuImages();
                        skuImages.setSkuId(skuId);
                        skuImages.setImgUrl(subItem.getImgUrl());
                        skuImages.setDefaultImg(Long.valueOf(subItem.getDefaultImg()));
                        skuImagesMapper.insertSkuImages(skuImages);
                    });

            // skuAttrValue
            item.getAttr().forEach(subItem -> {
                SkuAttrValue skuAttrValue = new SkuAttrValue();
                skuAttrValue.setSkuId(skuId);
                skuAttrValue.setAttrId(subItem.getAttrId());
                skuAttrValue.setAttrName(subItem.getAttrName());
                skuAttrValue.setAttrValue(subItem.getAttrValue());
                skuAttrValueMapper.insertSkuAttrValue(skuAttrValue);
            });

            /**
             * 操作商品的优惠、满减等信息
             */
            // skuLadder
            SkuLadder skuLadder = new SkuLadder();
            skuLadder.setSkuId(skuId);
            skuLadder.setFullCount(Long.valueOf(item.getFullCount()));
            skuLadder.setDiscount(BigDecimal.valueOf(item.getDiscount()));
            skuLadder.setAddOther(item.getCountStatus());
            remoteCouponService.saveSkuLadder(skuLadder);

            // skuFullReduction
            SkuFullReduction skuFullReduction = new SkuFullReduction();
            skuFullReduction.setSkuId(skuId);
            skuFullReduction.setFullPrice(BigDecimal.valueOf(item.getFullPrice()));
            skuFullReduction.setReducePrice(BigDecimal.valueOf(item.getReducePrice()));
            skuFullReduction.setAddOther(item.getPriceStatus());
            remoteCouponService.saveSkuFullReduction(skuFullReduction);

            // memberPrice
            item.getMemberPrice().forEach(subItem -> {
                MemberPrice memberPrice = new MemberPrice();
                memberPrice.setSkuId(skuId);
                memberPrice.setMemberLevelId(Long.valueOf(subItem.getId()));
                memberPrice.setMemberLevelName(subItem.getName());
                memberPrice.setMemberPrice(BigDecimal.valueOf(subItem.getPrice()));
                memberPrice.setAddOther(1);
                remoteCouponService.saveMemberPrice(memberPrice);
            });


        });

        // spuBounds 积分
        SpuBounds spuBounds = new SpuBounds();
        spuBounds.setSpuId(spuId);
        spuBounds.setBuyBounds(BigDecimal.valueOf(productDTO.getBounds().getBuyBounds()));
        spuBounds.setGrowBounds(BigDecimal.valueOf(productDTO.getBounds().getGrowBounds()));
        remoteCouponService.saveSpuBounds(spuBounds);
    }

    /**
     * 修改spu信息
     * 
     * @param spuInfo spu信息
     * @return 结果
     */
    @Override
    public int updateSpuInfo(SpuInfo spuInfo)
    {
        spuInfo.setUpdateTime(DateUtils.getNowDate());
        return spuInfoMapper.updateSpuInfo(spuInfo);
    }

    /**
     * 批量删除spu信息
     * 
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deleteSpuInfoByIds(Long[] ids)
    {
        return spuInfoMapper.deleteSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     * 
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deleteSpuInfoById(Long id)
    {
        return spuInfoMapper.deleteSpuInfoById(id);
    }
}
